﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Domain;
using System.Data.SqlClient;

namespace Repository
{
    public class FornecedorDB : IQuery<Fornecedor>
    {
        private SqlConnection connection;

        public FornecedorDB(SqlConnection connection) 
        {
            this.connection = connection;
        }



        public bool save(Fornecedor e)
        {
            try
            {
                string sql = "insert into fornecedor (nome,cnpj,telefone) values ('"+e.Nome+"','"+e.Cnpj+"','"+e.Telefone+"')";
                SqlCommand command = new SqlCommand(sql,this.connection);
                command.ExecuteNonQuery();
                return true;

            }
            catch (Exception)
            {
                return false;
            }
        }

        public bool update(Fornecedor e)
        {
            throw new NotImplementedException();
        }

        public List<Fornecedor> getAllList()
        {
            try
            {
                string sql = "select * from fornecedor";
                SqlCommand command = new SqlCommand(sql, this.connection);
                SqlDataReader dr = command.ExecuteReader();
                List<Fornecedor> fornecedores = new List<Fornecedor>();
                while (dr.Read()) 
                {
                    Fornecedor f = new Fornecedor();
                    f.Id_fornecedor = Convert.ToInt32(dr["id_fornecedor"]);
                    f.Nome = Convert.ToString(dr["nome"]);
                    f.Telefone = Convert.ToString(dr["telefone"]);
                    f.Cnpj = Convert.ToString(dr["cnpj"]);
                    fornecedores.Add(f);
                }
                return fornecedores;
            }
            catch (Exception)
            {
                return null;
            }
        }

        public List<Fornecedor> getListById(int id)
        {
            try
            {
                string sql = "select * from fornecedor where id_fornecedor = "+id+" ";
                SqlCommand command = new SqlCommand(sql, this.connection);
                SqlDataReader dr = command.ExecuteReader();
                List<Fornecedor> fornecedores = new List<Fornecedor>();
                while (dr.Read())
                {
                    Fornecedor f = new Fornecedor();
                    f.Id_fornecedor = Convert.ToInt32(dr["id_fornecedor"]);
                    f.Nome = Convert.ToString(dr["nome"]);
                    f.Telefone = Convert.ToString(dr["telefone"]);
                    f.Cnpj = Convert.ToString(dr["cnpj"]);
                    fornecedores.Add(f);
                }
                return fornecedores;
            }
            catch (Exception)
            {
                return null;
            }
        }

        public bool delete(Fornecedor e)
        {
            throw new NotImplementedException();
        }

        public void openConnection()
        {
            this.connection.Open();
        }

        public void closeConnection()
        {
            this.connection.Close();
        }

        public void putConnection(System.Data.SqlClient.SqlConnection connection)
        {
            this.connection = connection;
        }
    }
}
